|
1
|
|
|
/* |
|
2
|
|
|
* grunt-guetzli |
|
3
|
|
|
* https://github.com/Ayesh/grunt-guetzli |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/* |
|
9
|
|
|
--quality <float> ............. quality factor (84..100) |
|
10
|
|
|
--verbose ............... print version number and exit. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
'use strict'; |
|
14
|
|
|
|
|
15
|
|
|
module.exports = function(grunt) { |
|
16
|
|
|
var path = require('path'); |
|
17
|
|
|
var async = require('async'); |
|
18
|
|
|
var fs = require('fs'); |
|
19
|
|
|
grunt.registerMultiTask('guetzli', "Compresses JPG and PNG images according to Google's Guetzli algorithm", function() { |
|
20
|
|
|
/** |
|
21
|
|
|
* Retrieves defined options. |
|
22
|
|
|
*/ |
|
23
|
|
|
var options = this.options(); |
|
24
|
|
|
grunt.verbose.writeflags(options, 'Options'); |
|
25
|
|
|
|
|
26
|
|
|
var done = this.async(); |
|
27
|
|
|
|
|
28
|
|
|
var guetzli_bin = 'guetzli'; |
|
29
|
|
|
if (options.binpath) { |
|
30
|
|
|
guetzli_bin = options.binpath; |
|
31
|
|
|
} |
|
32
|
|
|
var source_total = 0; |
|
33
|
|
|
var dest_total = 0; |
|
34
|
|
|
var oversize_total = 0; |
|
35
|
|
|
|
|
36
|
|
|
// Iterate over all src-dest file pairs. |
|
37
|
|
|
async.eachSeries(this.files, function(f, next) { |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create folder for the dest file |
|
41
|
|
|
*/ |
|
42
|
|
|
grunt.file.mkdir(path.dirname(f.dest)); |
|
43
|
|
|
var args = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Quality scale: Minimum allowed is 84. |
|
47
|
|
|
*/ |
|
48
|
|
|
if (options.quality) { |
|
49
|
|
|
if (options.quality < 84) { |
|
50
|
|
|
grunt.fail.warn('Guetzli: Quality must be >= 84 to avoid noticeable artifacts.'); |
|
51
|
|
|
} |
|
52
|
|
|
args.push('--quality'); |
|
53
|
|
|
args.push(options.quality); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Outputs the rules that have been matched. |
|
58
|
|
|
*/ |
|
59
|
|
|
if (options.verbose) { |
|
60
|
|
|
args.push('--verbose'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
args.push(f.src); |
|
64
|
|
|
args.push(f.dest); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Outputs the file that is being analysed. |
|
68
|
|
|
*/ |
|
69
|
|
|
grunt.log.subhead('Compressing: ' + f.dest); |
|
70
|
|
|
var child = grunt.util.spawn({ |
|
71
|
|
|
cmd: guetzli_bin, |
|
72
|
|
|
args: args |
|
73
|
|
|
}, function(error, result, code) { |
|
74
|
|
|
//grunt.log.writeln(code+''+result); |
|
75
|
|
|
if (code !== 0) { |
|
76
|
|
|
return grunt.warn(String(code)); |
|
77
|
|
|
} |
|
78
|
|
|
else{ |
|
79
|
|
|
var source = fs.statSync(f.src[0])['size']; |
|
80
|
|
|
var dest = fs.statSync(f.dest)['size']; |
|
81
|
|
|
var diff = ((source - dest) / source) * 100; |
|
82
|
|
|
diff = Number((diff).toFixed(2)); |
|
83
|
|
|
source_total += source; |
|
84
|
|
|
if (diff < 0) { |
|
85
|
|
|
oversize_total++; |
|
86
|
|
|
source_total += source; |
|
87
|
|
|
diff = diff * -1; |
|
88
|
|
|
if (options.deleteLarger) { |
|
89
|
|
|
grunt.file.delete(f.dest); |
|
90
|
|
|
grunt.log.writeln('Deleted: '['yellow'] + diff + '% larger than its source.'); |
|
91
|
|
|
} |
|
92
|
|
|
else { |
|
93
|
|
|
dest_total += dest; |
|
94
|
|
|
grunt.log.writeln('Warning: '['yellow'] + diff + '% larger than its source. Left undeleted.'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
else { |
|
98
|
|
|
dest_total += dest; |
|
99
|
|
|
grunt.log.oklns('Done: '['green'] + diff + '% smaller | ' + diff + '%: ' + source + ' -> ' + dest); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
next(error); |
|
|
|
|
|
|
104
|
|
|
}); |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* displays the output and error streams via the parent process. |
|
108
|
|
|
*/ |
|
109
|
|
|
child.stdout.pipe(process.stdout); |
|
110
|
|
|
child.stderr.pipe(process.stderr); |
|
111
|
|
|
|
|
112
|
|
|
}.bind(this), function() { |
|
|
|
|
|
|
113
|
|
|
var total_diff = (source_total - dest_total) / source_total; |
|
114
|
|
|
var total_diff_perentage = Number((total_diff * 100).toFixed(2)); |
|
115
|
|
|
grunt.log.subhead('Operation statistics:'); |
|
116
|
|
|
grunt.log.oklns(source_total + ' -> ' + dest_total); |
|
117
|
|
|
grunt.log.oklns(total_diff_perentage +'% saved.'); |
|
118
|
|
|
if (oversize_total !== 0) { |
|
119
|
|
|
if (options.deleteLarger) { |
|
120
|
|
|
grunt.log.oklns('Deleted ' + oversize_total + ' file(s) due to larger output.'); |
|
121
|
|
|
} |
|
122
|
|
|
else { |
|
123
|
|
|
grunt.log.oklns('Warning: ' + 'Contains ' + oversize_total + ' file(s) larger than their sources.'); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
done(); |
|
127
|
|
|
}); |
|
128
|
|
|
|
|
129
|
|
|
}); |
|
130
|
|
|
}; |
|
131
|
|
|
|